/*
-> setAttrib ('onChange', $this->go());
go() ist funktion

bei mir setGeo()
-> setAttrib ('onChange', $this->setGeo());
name
land
stadt
*/

var map;
var geocoder;
//var markersArray = [];
//var markerme = new google.maps.Marker();
var marker = new google.maps.Marker();
//Nachkommastellenanzahl
var myCoordsLenght = 6;

function initialize() {
  var haightAshbury = new google.maps.LatLng(48.178932, 16.387656);
  var mapOptions = {
    zoom: 12,
    center: haightAshbury,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };
  geocoder = new google.maps.Geocoder();
  map =  new google.maps.Map(document.getElementById("canvas"), mapOptions);
  

  
  google.maps.event.addListener(map, 'click', function(event) {
    marker.setMap(null);
	marker = addMarker(event.latLng);
	//map.setCenter(marker);	-->FEHLER
	document.getElementById('lat').value = event.latLng.lat().toFixed(myCoordsLenght);
    document.getElementById('lng').value = event.latLng.lng().toFixed(myCoordsLenght);
	var latlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
	geocoder.geocode({'latLng': latlng}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			if (results[6]) {
				var input = results[6].formatted_address;
				var latlngStr = input.split(", ");
				var land = latlngStr[latlngStr.length-1];
				var stadt = '';
				if (latlngStr.length >= 3){
					stadt = latlngStr[latlngStr.length-3];
				}
				//document.getElementById('lat').value = event.latLng.lat().toFixed(myCoordsLenght);
				//document.getElementById('lng').value = event.latLng.lng().toFixed(myCoordsLenght);
				document.getElementById('land').value = land;
				document.getElementById('stadt').value = stadt;
				document.getElementById('revier').value = results[6].formatted_address;
				//document.getElementById('revier').value = results[6].formatted_address;
			}
		} 
		else {
			//document.getElementById('stadt').value = "fehler";
			alert("Geocoder failed due to: " + status);
		}    
	});
	map.setCenter(marker.getPosition());
  });
  //map.setCenter(marker);
}
function addMarker(location) {
  marker = new google.maps.Marker({
    position: location,
    map: map,
	dragable: true
  });
  //map.setCenter(marker);
  return marker;
}
function setGeo(feldname){
	//alert('setGeo: '+ feldname);
	var myzoom = 5;
	var address = document.getElementById(feldname).value;
	//alert("Adresse:" );
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			marker.setMap(null);
			marker = addMarker(results[0].geometry.location);
			map.setCenter(marker.getPosition());
			document.getElementById('lat').value = marker.getPosition().lat().toFixed(myCoordsLenght);
			document.getElementById('lng').value = marker.getPosition().lng().toFixed(myCoordsLenght);
			if(feldname=='land'){
				document.getElementById('stadt').value = '';
				document.getElementById('land').value = address;
				myzoom = 7;
			}
			else if(feldname=='stadt'){
				var latlng = marker.getPosition();
				geocoder.geocode({'latLng': latlng}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[6]) {
							document.getElementById('revier').value = results[6].formatted_address;
							var input = results[6].formatted_address;
							var latlngStr = input.split(", ");
							document.getElementById('land').value = latlngStr[latlngStr.length-1];
							document.getElementById('stadt').value = address;
						}
						else{
							alert("Geocoder can not output Addressinformation: "+  status);
						}
					} 
					else {
						alert("Geocoder failed due to: " + status);
					}    
				});
				myzoom = 12;
			}
			else{
				document.getElementById('stadt').value ='';
				document.getElementById('land').value ='';
				alert("Geocode was not successful, please try again!");
			}
			map.setZoom(myzoom);
		}
		else {
			alert("Geocode was not successful for the following reason: " + status);
		}
    });
}
/*function deleteMarker(){
	for (i in markerarray) {
      markerarray[i].setMap(null);
    }
	markerarray.length = 0;
}
function addMarker(location){
	deleteMarker();
	var marker = new google.maps.Marker({
		position: location,
		map: map
	}
	markerarray.push(marker);
}*/




